﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using System;
using Tesla.Collections;
using Tesla.Core;

namespace Tesla.Input {
    /// <summary>
    /// InputLayer manages a set of input triggers and is used to logically organize triggers.
    /// </summary>
    public sealed class InputLayer {
        private TeslaList<InputTrigger> _triggers;
        private bool _isEnabled;

        /// <summary>
        /// Get the collection of triggers associated with this input layer.
        /// </summary>
        public ReadOnlyList<InputTrigger> Triggers {
            get {
                return _triggers.AsReadOnly();
            }
        }

        /// <summary>
        /// Get or set if this input layer is enabled.
        /// </summary>
        public bool IsEnabled {
            get {
                return _isEnabled;
            }
            set {
                _isEnabled = false;
            }
        }

        /// <summary>
        /// Creates a new instance of <see cref="InputLayer"/> which is by default enabled.
        /// </summary>
        public InputLayer() {
            _triggers = new TeslaList<InputTrigger>();
            _isEnabled = true;
        }

        /// <summary>
        /// Register a new trigger to the layer to track and update.
        /// </summary>
        /// <param name="trigger">InputTrigger to register</param>
        /// <returns>Index that the trigger was added at</returns>
        public int RegisterTrigger(InputTrigger trigger) {
            if(trigger == null) {
                return -1;
            }
            _triggers.Add(trigger);
            return _triggers.Count - 1;
        }

        /// <summary>
        /// Remove a trigger from the layer.
        /// </summary>
        /// <param name="trigger">InputTrigger to remove</param>
        /// <returns>True if the trigger was removed</returns>
        public bool RemoveTrigger(InputTrigger trigger) {
            return _triggers.Remove(trigger);
        }

        /// <summary>
        /// Remove a trigger from the layer at the specified index.
        /// </summary>
        /// <param name="index">Zero-based index of the trigger in the InputTriggerCollection</param>
        /// <returns>True if the operation succeeded, false if an exception was thrown</returns>
        public bool RemoveTrigger(int index) {
            try {
                _triggers.RemoveAt(index);
                return true;
            } catch(ArgumentOutOfRangeException e) {
                System.Diagnostics.Debug.WriteLine(e.StackTrace);
                return false;
            }
        }

        /// <summary>
        /// Removes all triggers from the layer.
        /// </summary>
        public void RemoveAllTriggers() {
            _triggers.Clear();
        }

        /// <summary>
        /// Enables or disables a specific trigger using the specified index.
        /// </summary>
        /// <param name="index">Zero-based index of the trigger in the InputTriggerCollection</param>
        /// <param name="enabled">Enabled status of the trigger</param>
        /// <returns>True if the operation was successful false if an exception was thrown or if the trigger was null</returns>
        public bool EnableTrigger(int index, bool enabled) {
            try {
                InputTrigger trigger = _triggers[index];
                if(trigger != null) {
                    trigger.IsEnabled = enabled;
                    return true;
                }
                return false;
            } catch(ArgumentOutOfRangeException e) {
                System.Diagnostics.Debug.WriteLine(e.StackTrace);
                return false;
            }
        }

        /// <summary>
        /// Checks each trigger if its condition is true, then performs it.
        /// </summary>
        /// <param name="time">Time elapsed since the last update</param>
        public void CheckTriggers(GameTime time) {
            if(_isEnabled) {
                for(int i = 0; i < _triggers.Count; i++) {
                    _triggers[i].CheckAndPerform(time);
                }
            }
        }
    }
}
